/****************************************************************************
** @brief
*****************************************************************************/

#ifndef BASERECORD_H
#define BASERECORD_H

#include <QObject>
#include <QMutex>
#include <QSqlDatabase>

class BaseRecord : public QObject
{
    Q_OBJECT

public:
    explicit BaseRecord(QObject *parent = nullptr);
    ~BaseRecord();

    ///
    /// \brief InitRecorDB
    /// \param db_name
    ///
    void InitRecorDB(QString db_name);
    ///
    /// \brief CreateRecordTable
    /// \param table_name
    /// \param table_field
    ///
    void CreateRecordTable(QString table_name, QString table_field);
    ///
    /// \brief IsDBOpen
    /// \return
    ///
    bool IsDBOpen();

    ///
    /// \brief InsertRecord 创建记录
    /// \param vhash
    ///
    void InsertRecord(QVariantHash vhash);
    ///
    /// \brief DeleteRecord 删除记录
    /// \param targetKey
    /// \param targetValue
    ///
    void DeleteRecord(QString targetKey, QVariant targetValue);
    ///
    /// \brief UpdateRecord 更新记录
    /// \param vhash
    /// \param targetKey
    /// \param targetValue
    ///
    void UpdateRecord(QVariantHash vhash, QString targetKey, QVariant targetValue);
    void UpdateRecord(QString exitsKey, QVariant exitsValue, QString targetKey, QVariant targetValue);
    ///
    /// \brief IsRecordExist 检查满足条件的记录是否存在
    /// \param targetKey
    /// \param targetValue
    /// \return
    ///
    bool IsRecordExist(QString targetKey, QVariant targetValue);
    ///
    /// \brief RecordExistData 获取满足条件的记录
    /// \param targetKey
    /// \param targetValue
    /// \return
    ///
    QVariantHash RecordExistData(QString targetKey, QVariant targetValue);

    const QString last_error() const
    {
        return last_error_;
    }

protected:

private:
    QMutex mutex_;
    QSqlDatabase sql_db_;
    QString db_name_;
    QString table_name_;
    QString last_error_;

private:
    void ConnectDB();
    void OpenDB();
    void CloseDB();
    void CreateTable(QString field);

signals:

public slots:

private slots:

};

#endif // BASERECORD_H
